﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

using XGameFrame;
using XGameFrame.Graphic;

namespace XTowerDef
{
    public class CreepParticleEngine : ParticleEngineBase
    {
        #region Fields Region
        private AnimatedCreepSprite parent;
        #endregion

        #region Property Region
        public AnimatedCreepSprite Parent
        {
            get { return parent; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor for a new CreepParticleEngine which contains Particles
        /// </summary>
        /// <param name="pTextures">Textures which should be part of the particleeffect</param>
        /// <param name="pLocation">Location</param>
        public CreepParticleEngine(AnimatedCreepSprite pParent, Microsoft.Xna.Framework.Content.ContentManager pGameContent)
            :base(pParent.SizeOneFrame)
        {
            parent = pParent;
            Content = pGameContent;
            Textures.Add(Content.Load<Texture2D>(@"GameGraphics\Particles\CreepParticles\CreepParticles"));
            Textures.Add(Content.Load<Texture2D>(@"GameGraphics\Particles\CreepParticles\CreepParticles2"));
            Textures.Add(Content.Load<Texture2D>(@"GameGraphics\Particles\CreepParticles\CreepParticles3"));
        }
        #endregion

        #region Abstract Method Region
        /// <summary>
        /// Do Logic for Particles
        /// </summary>
        public override void Update(GameTime pGameTime)
        {
            this.EmitterLocation = Parent.TopCenter;

            int total = 10;

            for (int _i = 0; _i < total; _i++)
            {
                Particles.Add(GenerateNewParticle());
            }

            for (int _particle = 0; _particle < Particles.Count; _particle++)
            {
                Particles[_particle].Update(pGameTime);
                if (Particles[_particle].TTL <= 0)
                {
                    Particles.RemoveAt(_particle);
                    _particle--;
                }
            }

        }

        /// <summary>
        /// Do drawings for Particles
        /// </summary>
        /// <param name="pSpriteBatch">The graphic to draw</param>
        /// <param name="pLocation">The location where it's have to draw</param>
        public override void Draw(SpriteBatch pSpriteBatch)
        {
            for (int index = 0; index < Particles.Count; index++)
            {
                Particles[index].Draw(pSpriteBatch);
            }
        }

        #endregion

        #region Private Methods Region
        public virtual CreepParticle GenerateNewParticle()
        {
            Texture2D texture = Textures[Random.Next(Textures.Count)];
            Vector2 velocity = new Vector2(
                    1f * (float)(Random.NextDouble() * 2 - 1),
                    1f * (float)(Random.NextDouble() * 2 - 1));
            float angle = 0;
            float angularVelocity = 0.1f * (float)(Random.NextDouble() * 2 - 1);
            Color color = new Color(
                    (float)Random.NextDouble(),
                    (float)Random.NextDouble(),
                    (float)Random.NextDouble());
            float size = (float)Random.NextDouble();
            int ttl = 20 + Random.Next(40);

            return new CreepParticle(texture, this, velocity, angle, angularVelocity, color, size, ttl);
        }
        #endregion
    }
}
